define('sendPhoneCodeView',['common'],function(utils){
	var sendPhoneCodeView = (function(data,suc){
		utils.ajax(utils.getWebUrl('verify/sendPhoneCode'),
			data,
			'POST',
		function(res){
			suc(res);
		});	
	});
	return sendPhoneCodeView;
})	

define(['vue','config','common','sendPhoneCodeView'],function(Vue,config,utils,sendPhoneCodeView){
	'use strict';

	var addBank_realNameView=new Vue({
		el:'#app',
		data:{
			cardNo:'',
			banks:'',
			branch:'',
			phone:'',
			userPhone:'',
			phoneCode:'',
			secondOfDelay : 120000 ,
			secondLeftOfDelay : 0 ,
			submitBtn: false
		},
		created:function(){
			utils.userToken();

			var _this=this;
			utils.ajax(utils.getWebUrl('user/personal'),null,'POST',function(res){
				//console.log(res)
				_this.userPhone = res.data.phone;
				_this.phone = res.data.phone;
			},function(){
				$('.DataLoading').show();
				
			},function(){
				$('.DataLoading').hide();
			})
		},
		methods:{
			back:function(){
				history.back();
				//window.location.href="certification.html";
			},
			inputPhone:function(event){
				//console.log(this.phone)
				if(this.phone.length === 11){
					if(this.phone !== this.userPhone){
						this.fadeIn();
					}else{
						this.fadeOut();
					}
				}
			},
			bankcard:function(event){
				var obj = event;
				var _this = this;
	            if(obj.keyCode != 8){                                                        //判断是否为Backspace键，若不是执行函数；
	                  var b = _this.cardNo;        //定义变量input  value值；
	                  var maxValue = 26;                                                       //限制输入框的最大值；
	                  b = b.replace(/[^\d\s]/g,"");                                         //正则表达式：如果输入框中输入的不是数字或者空格，将不会显示；
	                  _this.cardNo = b;               //把新得到得value值赋值给输入框；
	                 for(var n=1;n<=5;n++){                                                
	                      if(b.length <=5*n-2||b.length>5*n-1){                    //判断是否是该加空格的时候，若不会，还是原来的值；
	                          b = b;
	                      }else{
	                          b = b +" ";                                                           //给value添加一个空格；
	                          _this.cardNo = b;    //赋值给输入框新的value值；
	                      }
	                  }                     
	            }
			},
			getVeryCode:function(){
				var _this=this;
				var cardNo = utils.Trim(this.cardNo,"g");
				if($.trim(cardNo) === ""){
					utils.appAlert('银行卡号不能为空');
					return;
				}
				// if($.trim(this.banks) === ""){
				// 	utils.appAlert('开户行不能为空');
				// 	return;
				// }
				if($.trim(this.branch) === ""){
					utils.appAlert('支行名称不能为空');
					return;
				}
				if($.trim(this.phone) === ""){
					utils.appAlert('手机号码不能为空');
					return;
				}
				sendPhoneCodeView({
					phone: _this.phone,
					codeType: 7,
					smsType: 1
				},function(res){
					utils.appAlert('请查收你的短信',function(){
			        	_this.sendVerifyCodeTimer();
			        	$(".verification").attr('disabled','true');
			        });	
				})
			},
			sendVerifyCodeTimer:function(){
				var $time_debounce = $('.verification');
				var _this=this;
				_this.setTimeToDefaut = function(secondsOfInterval) {
                	this.secondLeftOfDelay = secondsOfInterval;
            	};

				var secondsOfInterval = this.secondOfDelay;

				var intervalID = setInterval(function() {
	                secondsOfInterval = secondsOfInterval - 1000;
	                $time_debounce.html(" " + secondsOfInterval / 1000 + " 秒 ")
	                if (secondsOfInterval <= 0) {
	                    clearInterval(intervalID);
	                    $(".verification").removeAttr("disabled");
	                    $time_debounce.html("获取验证码");
	                }
	                _this.setTimeToDefaut(secondsOfInterval);
	            }, 1000);
			},
			/*getvoiceMessage:function(){
				var _this=this;
				var cardNo = utils.Trim(this.cardNo,"g");
				if($.trim(cardNo) === ""){
					utils.appAlert('银行卡号不能为空');
					return;
				}
				if($.trim(this.banks) === ""){
					utils.appAlert('开户行不能为空');
					return;
				}
				if($.trim(this.branch) === ""){
					utils.appAlert('支行名称不能为空');
					return;
				}
				if($.trim(this.phone) === ""){
					utils.appAlert('手机号码不能为空');
					return;
				}
				sendPhoneCodeView({
					phone: _this.phone,
					codeType: 7,
					smsType: 2
				},function(res){
					utils.appAlert('你正在使用语音获取验证码<br/>请保持手机通畅');
				});
			},*/
			submit:function(){
				var _this = this;
				var cardNo = utils.Trim(this.cardNo,"g");

				if($.trim(cardNo) === ""){
					utils.appAlert('银行卡号不能为空');
					return;
				}
				// if($.trim(this.banks) === ""){
				// 	utils.appAlert('开户行不能为空');
				// 	return;
				// }
				if($.trim(this.branch) === ""){
					utils.appAlert('支行名称不能为空');
					return;
				}
				if($.trim(this.phone) === ""){
					utils.appAlert('手机号码不能为空');
					return;
				}
				if(!(/^1(3|4|5|7|8)\d{9}$/.test(this.phone))){
                    utils.appAlert('请输入正确的手机号码');
                    return;
				}
				if((this.phone !== this.userPhone)&&($.trim(this.phoneCode)==="")){
					utils.appAlert('验证码不能为空')
					return;
				}

				if (_this.submitBtn) {
					return;
				}

				
				_this.submitBtn = true;
				var certification = utils.getData('certification');
				var data = {
							cardNo: cardNo,
							banks: '中国'+this.banks+'银行',
							branch: this.branch,
							phone: this.phone,
							phoneCode: this.phoneCode,
							cardType: 1
						}
				$.ajax({
	                url: utils.getWebUrl('bankard/add'),
	                type: 'POST',
	                data: data ,
	                dataType: 'json',
	                headers: {
	                   'authorization': 'Bearer '+ utils.userToken()
	                },
	                xhrFields: {
	                    withCredentials: true
	                },
	                crossDomain: true,
	                beforeSend:function(){
						$('body').append('<div class="msg">'+'审核中'+'</div>');
					},
	                success: function(res) {
	                    if(res.code === "10000"){
	                    	var type = utils.getQueryVariable('type');
	                    	utils.appAlert("恭喜!<br/>您已通过实名认证成功",function(){
	                    		switch(type){
									case 1:
										window.location.href = "Settings.html";
									  break;
									case 2:
										window.location.href = "personalData.html";
									  break;
									case 3:
										window.location.href = "withdraw.html";
									  break;
									case 4:
										window.location.href = "withdraw_firm.html";
									  break;
									default:
										window.location.href = "userInfo.html";
									}
	                    	},'确定') 
	                    }else{
	                    	_this.submitBtn = false;
	                        utils.appAlert("抱歉!<br/>实名认证未通过<br/>原因是: "+res.msg+"",null,"知道了");
	                    }
	                },
	                complete:function(){
						$('.msg').hide();
					},
	                error: function(res){
	                	_this.submitBtn = false;
	                    utils.appAlert('系统出现了错误,系统管理员正在修复');
	                }
	            });

			},
			fadeIn:function(){
				$('.Message').removeClass('out');
				$('.Message').addClass('pop');		
				$('.Message').show();
			},
			fadeOut:function(){
				$('.Message').addClass('out');
				$('.Message').removeClass('pop');
				setTimeout(function(){
					$('.Message').hide();
				},100)
			}
		}
	})

	return addBank_realNameView;
});